<?php
namespace app\controller;

use app\BaseController;

use think\facade\View;

require POD_PATH.'SqliteDB.php';

use pod;

class Udid extends BaseController
{

    public function index()
    {
        // 模板输出并变量赋值
        if (isset($_GET["udid"])) {
            return View::fetch('index', [
                'title' => '获取UDID',
                'udid'  => $_GET['udid'],
                'imei' => $_GET['imei'],
                'device' => $_GET['device'],
                'deviceversion' => $_GET['deviceversion'],
                'devicename' => $_GET['devicename'],
                'username' => $_GET['username'],

            ]);
        }else{
            return View::fetch('config', []);
        }
    }


    public function review(){
        
            return View::fetch('review', ['title' => 'UDID列表',]);
    }

    public function device(){
        if (isset($_GET['d'])) {
            return $this->getDeviceName($_GET['d']);
        }
        return "";
    }

    public function getDeviceName($key='')
    {
        $content = file_get_contents(DATA_DIR."/apple_device.json");
        $arr = json_decode($content,true);
        $arrDevice = $arr["machineTypeName"];
        if (isset($key) && strlen($key)>0 && isset($arrDevice[$key])) {
            # code...
            try{
                return $arrDevice[$key];
            }catch(Exception $e){
                return $key;
            }
        }else{
            return $key;
        }
    }

    public function config()
    {
        header('Content-type: application/x-apple-aspen-config; chatset=utf-8');
        header('Content-Disposition: attachment; filename="udid.mobileconfig"');

        $mobileconfig = file_get_contents(DATA_DIR."/udid/udidSign.mobileconfig");
        echo $mobileconfig;
        //return $mobileconfig;
        exit();
    }

    public function save(){
        $req = $_POST;
        $username = isset($req['username']) ? $req['username'] : '';
        $device = isset($req['device']) ? $req['device'] : '';
        $devicename = isset($req['devicename']) ? $req['devicename'] : '';
        $udid = isset($req['udid']) ? $req['udid'] : '';
        $deviceversion = isset($req['deviceversion']) ? $req['deviceversion'] : '';
        $system = isset($req['system']) ? $req['system'] : '';
        $imei = isset($req['imei']) ? $req['imei'] : '';
        if (strlen($udid)>0) {
            $db = new pod\SqliteDB();
            $db -> save($username,$device,$devicename,$udid,$deviceversion,$system,$imei);
        }
        return json_encode($_POST);
    }

    public function udids(){
        $db = new pod\SqliteDB();
        $ret = $db->queryUdids();
        return json_encode(['data'=>$ret],JSON_UNESCAPED_UNICODE);
    }


    public function receive()
    {

        //接收数据
        $data = file_get_contents('php://input');

        //解析数据
        $plistBegin   = '<?xml version="1.0"';
        $plistEnd   = '</plist>';

        $pos1 = strpos($data, $plistBegin);
        $pos2 = strpos($data, $plistEnd);

        $data2 = substr ($data,$pos1,$pos2-$pos1);

        $xml = xml_parser_create();

        xml_parse_into_struct($xml, $data2, $vs);
        xml_parser_free($xml);

        $UDID = "";
        $IMEI = "";
        $DEVICE_NAME = "";
        $DEVICE_PRODUCT = "";
        $DEVICE_VERSION = "";
        $iterator = 0;

        $arrayCleaned = array();

        foreach($vs as $v){

            if($v['level'] == 3 && $v['type'] == 'complete'){
                $arrayCleaned[]= $v;
            }
            $iterator++;
        }

        $data = "";

        $iterator = 0;

        foreach($arrayCleaned as $elem){
            $data .= "\n==".$elem['tag']." -> ".$elem['value']."<br/>";
            switch ($elem['value']) {
                case "CHALLENGE":
                $CHALLENGE = $arrayCleaned[$iterator+1]['value'];
                break;
                case "IMEI":
                $IMEI = $arrayCleaned[$iterator+1]['value'];
                break;
                case "DEVICE_NAME":
                $DEVICE_NAME = $arrayCleaned[$iterator+1]['value'];
                break;
                case "PRODUCT":
                $DEVICE_PRODUCT = $arrayCleaned[$iterator+1]['value'];
                break;
                case "UDID":
                $UDID = $arrayCleaned[$iterator+1]['value'];
                break;
                case "VERSION":
                $DEVICE_VERSION = $arrayCleaned[$iterator+1]['value'];
                break;                       
            }
            $iterator++;
        }
        $devicename = $this -> getDeviceName($DEVICE_PRODUCT);
//$username",$device,$devicename,$udid,$system,$systemversion,$imei

        $data = array('udid' => $UDID, 
            'imei' => $IMEI,
            'username' => $DEVICE_NAME,
            'device' => $DEVICE_PRODUCT,
            'devicename' => $devicename,
            'devicetag' => $DEVICE_VERSION );

        dataLog(json_encode($data),"UDID","","Info");
//$username,$device,$devicename,$udid,$deviceversion,$system,$imei
        $params = "udid=".$UDID."&imei=".$IMEI."&username=".$DEVICE_NAME."&devicename=".$devicename."&device=".$DEVICE_PRODUCT."&deviceversion=".$DEVICE_VERSION;
//传值
header('HTTP/1.1 301 Moved Permanently');  //这里一定要301跳转,否则设备安装会提示"无效的描述文件"
return redirect(HOME_APP.'/public/index/udid/index?'.$params,301);


}

}
